import * as outService from '../../services/outOfStorage';
import { queryAllRoomList } from '../../services/tRoom';

export default {
  namespace: 'outOfStorage',
  state: {
    list: [],
    roomList: [],
    current: 1,
    currentSize: 10,
    total: 0,
  },
  reducers: {
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },
  },
  effects: {  
    *queryRoom({ payload }, { call, put }) {
      const { body } = yield call(queryAllRoomList, payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },  
    *queryList({ payload }, { call, put }) {
      const { body } = yield call(outService.queryList, payload);
      yield put({ type: 'queryListSuc', payload: { list: body.data, current: body.current, currentSize: body.size, total: body.iTotalRecords } });
    },

  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/outOfStorage') {        
          dispatch({ type: 'queryList', payload: {assetName:''} });
          dispatch({ type: 'queryRoom' });
        }
      });
    },
  },
};
